package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.PlaceService;

/**
 * Servlet implementation class CreateEventServlet
 */
@WebServlet("/CreateEventServlet")
public class CreateEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");  
		PrintWriter out = response.getWriter(); 
		PlaceService ps = new PlaceService();
		
		
		try{
						
			
			String namevent=request.getParameter("namevent");
			int price=Integer.parseInt(request.getParameter("price"));
			String description=request.getParameter("descriptions");
			int idplace=Integer.parseInt(request.getParameter("idplace"));
		
			int seats=Integer.parseInt(request.getParameter("seats"));
		Event event=new Event();
		
		
		Place place = ps.viewPlace(idplace);

        event.setNamevent(namevent);  
        event.setPrice(price); 
        event.setDescription(description); 
        event.setSeats(seats);
        event.setPlace(place);
		          
        EventService es=new EventService();
        
      	boolean tt =es.createEvent(event);
 		    if (tt==true){	
 		    RequestDispatcher rd3=request.getRequestDispatcher("mainpage2.jsp");  
 		    rd3.forward(request,response);
 		    
 		    }else{
 		    	 out.print("The event was not been created correctly");  
 		         RequestDispatcher rd2=request.getRequestDispatcher("neweventregistrationadd.jsp");  
 		         rd2.forward(request,response);  
 		         }
 	    }catch(Exception ex){
 			ex.getMessage();
 		}     
 		    out.close(); 
 
	          
	}

}
